package com.example.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

@RestController

@RequestMapping("/request")
public class request {
    @RequestMapping("/hello")
    public String func() {
        return "hello Spring Boot";
    }


    //获取cookie,两种方式,下面为传统写法
    //这种方式用的比较多,可以获取所有的cookie
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();

        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("bit".equals(cookie.getName())) {
                    //企业开发一般用这种方式,这种方式可以避免空指针
                    System.out.println(cookie.getName() + ":" + cookie.getValue());
                }
            }
        }
        return "获取cook成功";
    }

    //通过注解的方式拿到cookie,这种方式只能获取一个cookie
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("bit") String bite) {
        System.out.println(bite);
        return ("获取cookie成功");
    }


    //获取Session
    //设置session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        //参数为true,如果Session对象不存在,则创建一个session,如果存在,则直接返回
        HttpSession session = request.getSession(true);
//        if(session!=null){
//
//        }else {
//
//        }
        session.setAttribute("name", "zhangsan");
        session.setAttribute("id", "12343");
        return ("设置session成功");
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        session.setAttribute("name", "zhangsan");
        session.setAttribute("id", "12343");
        if (session != null) {
            System.out.println(session.getAttribute("name"));
            System.out.println(session.getAttribute("id"));
        }
        return "获取session成功";
    }

    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {//等同于 HttpSession
        // session = request.getSession();同样这种方式如果没有设置Session值,返回的是null
        System.out.println(session.getAttribute("name"));
        System.out.println(session.getAttribute("id"));
        return "获取到cookie";
    }

    //注解方式获取去Session
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute(value = "name", required = false) String name) {//这个注解做的步骤是session.getAttribute("name")
        //这里如果指定的是true,那么session中必须要有name的值
        System.out.println("name" + name);
        return "获取到cookie";
    }

    //获取header信息
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
        String header = request.getHeader("User-Agent");
        return header;
    }

    //注解获取header
    @RequestMapping("/getHeader2")
    public String getHeader(@RequestHeader("User-Agent") String userAgent) {
        return userAgent;
    }
}
